<!DOCTYPE html>

<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Edit Goods</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <script type="text/javascript" th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" id="fm" enctype="multipart/form-data">
                <div class="layui-form-item" style="display: none">
                    <label for="goodsNo" class="layui-form-label">
                        <span class="x-red">*</span>商品编号
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="goodsNo" name="goodsNo"
                               required="" lay-verify="userNo" autocomplete="off"
                               class="layui-input" th:value="${goods.goodsNo}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="goodsName" class="layui-form-label">
                        <span class="x-red">*</span>商品名称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="goodsName" name="goodsName"
                               required="" lay-verify="goodsName" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.goodsName}">>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="goodsPicture_btn" name="goodsPicture_btn">上传图片</button>
                        <input type="hidden" id="img_url" name="goodsPicture" value=""/>
                        <div class="layui-upload-list">
                            <img alt="这是图片" class="layui-upload-img" id="goodsPictureImg" th:src="@{/{filename}(filename=${goods.goodsPicture})}" src="">
                            <p id="goodsPictureText"></p>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="price" class="layui-form-label">商品价格</label>
                    <div class="layui-input-inline">
                        <input type="text" id="price" name="price"
                               required="" lay-verify="price" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.price}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="activity" class="layui-form-label">商品活动</label>
                    <div class="layui-input-inline">
                        <input type="text" id="activity" name="activity"
                               required="" lay-verify="activity" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.activity}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="deliverAddress" class="layui-form-label">商品发货地址</label>
                    <div class="layui-input-inline">
                        <input type="text" id="deliverAddress" name="deliverAddress"
                               required="" lay-verify="deliverAddress" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.deliverAddress}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="monthSale" class="layui-form-label">商品月售量</label>
                    <div class="layui-input-inline">
                        <input type="text" id="monthSale" name="monthSale"
                               required="" lay-verify="monthSale" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.monthSale}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="evaluate" class="layui-form-label">商品评价</label>
                    <div class="layui-input-inline">
                        <input type="text" id="evaluate" name="evaluate"
                               required="" lay-verify="evaluate" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.evaluate}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="discription" class="layui-form-label">商品描述</label>
                    <div class="layui-input-inline">
                        <input type="text" id="discription" name="discription"
                               required="" lay-verify="discription" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.discription}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="pageView" class="layui-form-label">商品浏览数</label>
                    <div class="layui-input-inline">
                        <input type="text" id="pageView" name="pageView"
                               required="" lay-verify="pageView" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.pageView}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="goodsStore" class="layui-form-label">商品店铺</label>
                    <div class="layui-input-inline">
                        <input type="text" id="goodsStore" name="goodsStore"
                               required="" lay-verify="goodsStore" autocomplete="off"
                               class="layui-input"
                               th:value="${goods.goodsStore}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="providerNo" class="layui-form-label">供应商编号</label>
                    <div class="layui-input-inline" style="width: 66%;">
                        <select  name="provider.providerNo" id="providerNo" lay-verify="required">
                            <option value="">[[${goods.provider.providerNo}]]</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <button id="L_repass" class="layui-btn" lay-filter="edit"
                            lay-submit="">修改</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    layui.use([ 'form', 'layer', 'jquery', 'upload' ], function() {
        $ = layui.jquery;
        var form = layui.form, layer = layui.layer, upload = layui.upload, laydate = layui.laydate;
        $.get("/goods/providerList",{},function (data) {
            var list = data;
            var select = document.getElementById('providerNo');
            if (list != null || list.size() > 0) {
                for (var c in list) {
                    var option = document.createElement("option");
                    option.setAttribute("value", list[c].providerNo);
                    option.innerText = list[c].providerNo;
                    select.appendChild(option)
                }
            }
            form.render('select');
        },"json");

        /*上传图片*/
        var uploadInst = upload.render({
            elem: '#goodsPicture_btn',
            url: '/upload',
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    console.log(file);
                    // console.log(result);
                    $('#goodsPictureImg').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.status > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                console.log(res.code)
                if (res.code === '0') {
                    console.log(res.src)
                    $('#img_url').attr("value",res.src)
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var goodsPictureText = $('#goodsPictureText');
                goodsPictureText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                goodsPictureText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });


        //监听提交
        form.on('submit(edit)', function(data) {
            console.log(data.field)
            $.ajax({
                url : "/goods/edit",
                method : 'post',
                data : data.field,
                dataType : 'JSON',
                success : function(res) {
                    if (res.code === 0) {
                        //弹出提示，并在1秒后进行跳转
                        layer.alert("修改成功", {
                            icon : 6
                        }, function() {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                            return false;
                        });
                    } else {
                        layer.msg("修改失败:" + res.msg);
                        return false;
                    }
                }
            });
            return false;
        });
    })
</script>
</body>
</html>

